package licode.unisop.client.feign.impl;

import licode.unisop.client.api.SopPlatformAuth;
import licode.unisop.client.api.auth.AuthTokenShim;
import licode.unisop.client.feign.SopPlatformAuthFeign;
import licode.unisop.client.info.SopIdInfo;
import licode.unisop.client.info.SopPlatAuthInfo;
import licode.unisop.client.info.SopPlatHeartInfo;
import licode.unisop.client.utils.MethodCallUtil;
import licode.unisop.client.vo.SopAuthPlatform;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author licode
 */
@Component
public class SopPlatformAuthFeignImpl implements SopPlatformAuth {
    @Resource
    private AuthTokenShim authTokenShim;

    @Resource
    private SopPlatformAuthFeign sopPlatformAuthFeign;

    private String modalToken(String platformId) {
        return authTokenShim.modalToken(platformId);
    }

    @Override
    public SopAuthPlatform register(SopPlatAuthInfo authInfo) {
        if (null == authInfo.getRealmId()) {
            authInfo.setRealmId("default");
        }
        return MethodCallUtil.execFeign(() -> sopPlatformAuthFeign.register(authInfo));
    }

    @Override
    public SopAuthPlatform fromId(SopIdInfo idInfo) {
        return MethodCallUtil.execFeign(() ->
                sopPlatformAuthFeign.fromId(modalToken(idInfo.getId()), idInfo.getId(), idInfo));
    }

    @Override
    public long getAuthCount(SopIdInfo idInfo) {
        return MethodCallUtil.execFeign(() ->
                sopPlatformAuthFeign.getAuthCount(modalToken(idInfo.getId()), idInfo.getId()));
    }

    @Override
    public void forbid(SopIdInfo idInfo) {
        MethodCallUtil.execFeign(() ->
                sopPlatformAuthFeign.forbid(modalToken(idInfo.getId()), idInfo.getId(), idInfo));
    }

    @Override
    public String renew(SopPlatHeartInfo req) {
        return MethodCallUtil.execFeign(() ->
                sopPlatformAuthFeign.renew(modalToken(req.getPlatformId()), req.getPlatformId(), req));
    }
}
